package ALG;

public class SlidingWindow {

    public static void main(String[] args) {

        int []nums ={1,1,1,0,0,0,1,1,1,1,0};
        int K = 3;

        int i = longestOnescopy(nums, K);

        System.out.println(i);
    }

    public static int longestOnes(int[] nums, int K) {

        int left=0,right=0;
        int len=nums.length;
        int lsum=0,rsum=0;//前缀和
        int res=0;
        for(right=0;right<len;right++){
            rsum+=1-nums[right];//0变1，1变0
            while(rsum-lsum>K){ //不满足条件
                lsum+=1-nums[left];//0变1，1变0
                left++;//左指针右移
            }
            res=Math.max(res,right-left+1);
        }
        return res;
    }

    public static int longestOnescopy(int[] nums, int K) {

        int left = 0, right = 0;
        int len = nums.length;
        int res = 0;
        int ks = 0;
        for (right = 0; right < len; right++) {
            if (nums[right] == 0) {
                ks++;
            }
            while (ks > K) {
                if (nums[left] == 0) {
                    ks--;
                }
                left++;

            }
            res = Math.max(res, right - left + 1);
        }
        return res;
    }
}
